package cn.iocoder.yudao.module.bill.controller.admin.bill_cate;

import cn.iocoder.yudao.module.bill.controller.admin.common.CategoryNode;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.bill.controller.admin.bill_cate.vo.*;
import cn.iocoder.yudao.module.bill.dal.dataobject.bill_cate.BillCateDO;
import cn.iocoder.yudao.module.bill.service.bill_cate.BillCateService;

@Tag(name = "管理后台 - 账单分类")
@RestController
@RequestMapping("/bill/cate")
@Validated
public class BillCateController {

    @Resource
    private BillCateService cateService;

    @PostMapping("/create")
    @Operation(summary = "创建账单分类")
    @PreAuthorize("@ss.hasPermission('bill:cate:create')")
    public CommonResult<Long> createCate(@Valid @RequestBody BillCateSaveReqVO createReqVO) {
        return success(cateService.createCate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新账单分类")
    @PreAuthorize("@ss.hasPermission('bill:cate:update')")
    public CommonResult<Boolean> updateCate(@Valid @RequestBody BillCateSaveReqVO updateReqVO) {
        cateService.updateCate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除账单分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bill:cate:delete')")
    public CommonResult<Boolean> deleteCate(@RequestParam("id") Long id) {
        cateService.deleteCate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得账单分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bill:cate:query')")
    public CommonResult<BillCateRespVO> getCate(@RequestParam("id") Long id) {
        BillCateDO cate = cateService.getCate(id);
        return success(BeanUtils.toBean(cate, BillCateRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得账单分类列表")
    @PreAuthorize("@ss.hasPermission('bill:cate:query')")
    public CommonResult<List<BillCateRespVO>> getCateList(@Valid BillCateListReqVO listReqVO) {
        List<BillCateDO> list = cateService.getCateList(listReqVO);
        return success(BeanUtils.toBean(list, BillCateRespVO.class));
    }

    @GetMapping("/tree")
    @Operation(summary = "获得账单分类树")
    @PreAuthorize("@ss.hasPermission('bill:cate:query')")
    public CommonResult<List<CategoryNode>> getCateTree(@Valid BillCateListReqVO listReqVO) {
        List<CategoryNode> list = cateService.getCateTree();
        return success(list);
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出账单分类 Excel")
    @PreAuthorize("@ss.hasPermission('bill:cate:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCateExcel(@Valid BillCateListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<BillCateDO> list = cateService.getCateList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "账单分类.xls", "数据", BillCateRespVO.class,
                        BeanUtils.toBean(list, BillCateRespVO.class));
    }

}